﻿using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SEAM.Data;
using SEAM.Data.Provider;
using SEAM.Data.SqlServer;
using SEAM.Data.Oracle;

namespace SEAM.Test.Common
{
    public class DataAccessProvider : IConnectionProvider
    {

        protected static StringDictionary _ConnectionStrings;
        protected static StringDictionary ConnectionStrings
        {
            get { return _ConnectionStrings; }
        }

        static DataAccessProvider()
        {
            _ConnectionStrings = new StringDictionary();

            _ConnectionStrings.Add(Database.ReadWrite + "_" + Mode.Local, "Data Source=localhost;Integrated Security=True;");
            _ConnectionStrings.Add(Database.ReadWrite + "_" + Mode.Development, "Data Source=devserver;Integrated Security=True;");
            _ConnectionStrings.Add(Database.ReadWrite + "_" + Mode.Test, "Data Source=testserver;Integrated Security=True;");
            _ConnectionStrings.Add(Database.ReadWrite + "_" + Mode.Production, "Data Source=prodserver;Integrated Security=True;");

            _ConnectionStrings.Add(Database.ReadOnly + "_" + Mode.Local, "Data Source=localserverRO;Integrated Security=True;");
            _ConnectionStrings.Add(Database.ReadOnly + "_" + Mode.Development, "Data Source=devserverRO;Integrated Security=True;");
            _ConnectionStrings.Add(Database.ReadOnly + "_" + Mode.Test, "Data Source=testserverRO;Integrated Security=True;");
            _ConnectionStrings.Add(Database.ReadOnly + "_" + Mode.Production, "Data Source=prodserverRO;Integrated Security=True;");

            _ConnectionStrings.Add(Database.Warehouse + "_" + Mode.Local, "Data Source=localserverDW;Integrated Security=True;");
            _ConnectionStrings.Add(Database.Warehouse + "_" + Mode.Development, "Data Source=devserverDW;Integrated Security=True;");
            _ConnectionStrings.Add(Database.Warehouse + "_" + Mode.Test, "Data Source=testserverDW;Integrated Security=True;");
            _ConnectionStrings.Add(Database.Warehouse + "_" + Mode.Production, "Data Source=prodserverDW;Integrated Security=True;");
        }

        #region IConnectionProvider Members

        public DataAccess GetDataAccess(params object[] args)
        {
            if (args == null || args.Length != 2)
                return null;
            else
            {
                string connKey = args[0] + "_" + args[1];
                if (((Database)args[1]) == Database.Warehouse)
                    return new OracleDataAccess(ConnectionStrings[connKey]);
                else if (ConnectionStrings.ContainsKey(connKey))
                    return new SqlDataAccess(ConnectionStrings[connKey]);
                else
                    throw new DataAccessException("Invalid Database Connection Request.", string.Format("{0} / {1}", args[0], args[1]));
            }
        }

        #endregion
    }

    public enum Database
    {
        ReadWrite,
        ReadOnly,
        Warehouse
    }

    public enum Mode
    {
        Local,
        Development,
        Test,
        Production
    }
}
